package com.zerdoor.service.impl;

import com.zerdoor.dao.UserCenterDao;
import com.zerdoor.service.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/7.
 */
@Service
public class UserCenterServiceImpl implements UserCenterService {
    @Autowired
    private UserCenterDao userCenterDao;

    @Override
    public List<Map<String, Object>> getUserReceiverTaskInfo(Integer userId) {
        return userCenterDao.getUserReceiverTaskInfo(userId);
    }

    @Override
    public List<Map<String, Object>> getUserSenderTaskInfo(Integer userId) {
        return userCenterDao.getUserSenderTaskInfo(userId);
    }

    @Override
    public List<Map<String, Object>> getArtArticleFornews() {
        return userCenterDao.getArtArticleFornews();
    }

    @Override
    public BigDecimal getUserIncome(Integer userId) {
        return userCenterDao.getUserIncome(userId);
    }

    @Override
    public BigDecimal getUserPayout(Integer userId) {
        return userCenterDao.getUserPayout(userId);
    }

    @Override
    public BigDecimal getUserWallet(Integer userId) {
        return userCenterDao.getUserWallet(userId);
    }

    @Override
    public List<Map<String, Object>> getArtistGoingTasks(Integer userId) {
        return userCenterDao.getArtistGoingTasks(userId);
    }

    @Override
    public List<Map<String, Object>> getArtistReceiverTasks(Integer userId) {
        return userCenterDao.getArtistReceiverTasks(userId);
    }

    @Override
    public List<Map<String, Object>> getArtistHistoryTasks(Integer userId) {
        return userCenterDao.getArtistHistoryTasks(userId);
    }

    @Override
    public List<Map<String, Object>> getArtistProOrder(Integer userId) {
        return userCenterDao.getArtistProOrder(userId);
    }

    @Override
    public List<Map<String, Object>> getArtistProByPhotoId(Map<String, Object> map) {
        return userCenterDao.getArtistProByPhotoId(map);
    }

    @Override
    public void addArtistPro(Map<String, Object> map) {
        userCenterDao.addArtistPro(map);
    }

    @Override
    public void updateArtistPro(Map<String, Object> map) throws Exception{
        userCenterDao.updateArtistPro(map);
    }

    @Override
    public Map<String, Object> initUpdateArtistPro(Map<String, Object> map) {
        return userCenterDao.initUpdateArtistPro(map);
    }

    @Override
    public void deletePro(Map<String, Object> map) throws Exception {
        userCenterDao.deletePro(map);
    }

    @Override
    public List<Map<String, Object>> getArtistPhotoes(Integer userId) {
        return userCenterDao.getArtistPhotoes(userId);
    }

    @Override
    public void addPhoto(Map<String, Object> map) {
        userCenterDao.addPhoto(map);
    }

    @Override
    public void updatePhoto(Map<String, Object> map) {
        userCenterDao.updatePhoto(map);
    }

    @Override
    public void deletePhoto(Map<String, Object> map) throws Exception{
        userCenterDao.deletePhoto(map);
    }

    @Override
    public Map<String, Object> getArtistInfo(Integer artistId) {
        return userCenterDao.getArtistInfo(artistId);
    }

    /**
     * 获取艺术家技能
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getArtistSkill() {
        return userCenterDao.getArtistSkill();
    }

    @Override
    public List<Map<String, Object>> getSecondBaseTasktype() {
        return userCenterDao.getSecondBaseTasktype();
    }

    @Override
    public void updateUserArtist(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserArtist(map);
    }

    @Override
    public void updateArtisInfo(Map<String, Object> map) {
        userCenterDao.updateArtisInfo(map);
    }


    @Override
    public List<Map<String, Object>> getPrincipalBidTasks(Integer userId) {
        return userCenterDao.getPrincipalBidTasks(userId);
    }

    @Override
    public List<Map<String, Object>> getPrincipalGoingTasks(Integer userId) {
        return userCenterDao.getPrincipalGoingTasks(userId);
    }

    @Override
    public List<Map<String, Object>> getPrincipalHistoryBidTasks(Integer userId) {
        return userCenterDao.getPrincipalHistoryBidTasks(userId);
    }


    @Override
    public Map<String, Object> getSingleUserInfoByUserId(Integer userId) {
        return userCenterDao.getSingleUserInfoByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getPayInfo(Integer userId) {
        return userCenterDao.getPayInfo(userId);
    }


    @Override
    public List<Map<String, Object>> getPayOutInfo(Integer userId) {
        return userCenterDao.getPayOutInfo(userId);
    }


    @Override
    public List<Map<String, Object>> getMessagesById(Integer userId) {
        return userCenterDao.getMessagesById(userId);
    }

    @Override
    public List<Map<String, Object>> getMessagesByReceiver(Map<String, Object> map) {
        return userCenterDao.getMessagesByReceiver(map);
    }

    @Override
    public int getTotalMessages(Integer userId) {
        return userCenterDao.getTotalMessages(userId);
    }

    @Override
    public void addUserMessage(Map<String, Object> map) throws Exception {
        userCenterDao.addUserMessage(map);
    }

    @Override
    public int updateUserMessageRead(Map<String, Object> map) throws Exception {
        return userCenterDao.updateUserMessageRead(map);
    }

    @Override
    public Map<String, Object> getUserBaileInfo(int baileId) {
        return userCenterDao.getUserBaileInfo(baileId);
    }

    @Override
    public void updateUserBaileInfo(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserBaileInfo(map);
    }

    @Override
    public List<Map<String, Object>> colloctionProction(int userId) {
        return userCenterDao.colloctionProction(userId);
    }

    @Override
    public List<Map<String, Object>> colloctionArtist(int userId) {
        return userCenterDao.colloctionArtist(userId);
    }

    @Override
    public List<Map<String, Object>> colloctionBaile(int userId) {
        return userCenterDao.colloctionBaile(userId);
    }

    @Override
    public List<Map<String, Object>> colloctionProject(int userId) {
        return userCenterDao.colloctionProject(userId);
    }

    @Override
    public void deleteColloction(int favoriteId, int userId) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("favoriteId", favoriteId);
        map.put("userId", userId);
        userCenterDao.deleteColloction(map);
    }

    @Override
    public void updatePassword(Map<String, Object> map) throws Exception {
        userCenterDao.updatePassword(map);
    }

    @Override
    public void updateUserHeadPortrait(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserHeadPortrait(map);
    }

    @Override
    public void updateUserRealName(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserRealName(map);
    }

    @Override
    public void updateUserEmail(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserEmail(map);
    }

    @Override
    public void updateUserPhone(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserPhone(map);
    }

    @Override
    public void updateUserqq(Map<String, Object> map) throws Exception {
        userCenterDao.updateUserqq(map);
    }

    @Override
    public int checkUserPhone(Map<String, Object> map) {
        Map<String, Object> result = userCenterDao.checkUserPhone(map);
        if(result == null) return 0;
        return Integer.parseInt(result.get("total").toString());
    }

    @Override
    public int checkUserEmail(Map<String, Object> map) {
        Map<String, Object> result = userCenterDao.checkUserEmail(map);
        if(result == null) return 0;
        return Integer.parseInt(result.get("total").toString());
    }

    @Override
    public Map<String, Object> getTotalPresenter(int userId) {
        return userCenterDao.getTotalPresenter(userId);
    }

    @Override
    public void updateAlipay(Map<String, Object> map) throws Exception{
         userCenterDao.updateAlipay(map);
    }

    @Override
    public BigDecimal addUserWithdraw(int userId,BigDecimal payOutPrice) throws Exception {
        Map<String,Object> map = new HashMap<String,Object>();
        //判断钱包金额是否足够
        Map<String,Object> mobj = userCenterDao.getLevelMoney(userId);
        if(mobj == null){
            throw new Exception("提现失败");
        }
        //钱包余额
        BigDecimal a1 = new BigDecimal(mobj.get("wallet").toString());
        //冻结金额
        BigDecimal a2 = new BigDecimal(mobj.get("freezeMoney").toString());
        //可提取金额
        BigDecimal a3 = a1.subtract(a2);
        if(a3.compareTo(payOutPrice) < 0){
            throw new Exception("钱包可用余额不足");
        }
        //通过验证
        //添加冻结金额
        BigDecimal a4 = a2.add(payOutPrice);
        map.put("userId",userId);
        map.put("freezeMoney",a4);
        userCenterDao.updateFreezeMoney(map);
        // 插入提现申请记录表
        map.put("withdraw_id",null);
        map.put("money",payOutPrice);
        userCenterDao.addUserWithdraw(map);
        return a4;
    }
}
